//时间2017年12月4日
//手机号注册页面的js
//http://www.yunnantese.com/ZITAOHUI/SMSverification?mobile=15611776202   紫陶汇的手机验证码接口
//点击验证码发送验证码**************************************************************************************/
var InterValObj; //timer变量，控制时间
var count = 60; //间隔函数，1秒执行
var curCount = ''; //当前剩余秒数
var base_url = 'http://www.yunnantese.com/';
function sendMessage(phone) {//发送验证码的函数
	var userMsg = JSON.parse(sessionStorage.userMsg);
	var unionID = userMsg.unionID;
	var code_val = $("#pic_code").val();
	$.ajax({
		type:"get",
		url:base_url+"ZITAOHUI/SMSverification",
		async:true,
		data:{
			mobile: phone,
			uid:unionID,
			code:code_val
		},
		beforeSend:function(){
			$("#phoneSendCode").attr("disabled", "true");//按钮不可用
		},
		success:function(res){
			if (res.code == 1) {
               $("#phoneSendCode").removeAttr("disabled"); //启用按钮
               countDown();
               alert("验证码发送成功");
            }else{
            	alert("图形验证码错误，请重试");
            	$("#phoneSendCode").removeAttr("disabled"); //启用按钮
            }
		}
	});
};
function SetRemainTime() {//timer处理函数
	if(curCount == 0) {
		$("#phoneSendCode").css('background', "#7D2534");
		window.clearInterval(InterValObj); //停止计时器
		$("#phoneSendCode").removeAttr("disabled"); //启用按钮
		$("#phoneSendCode").text("重新发送");
	} else {
		curCount--;
		$("#phoneSendCode").text(curCount + "s");
	}
};
function countDown() {//倒计时启动的函数
	curCount = count;　　 //设置button效果，开始计时
	$("#phoneSendCode").attr("disabled", "true");//按钮不可用
	$("#phoneSendCode").css('background', "#999");
	$("#phoneSendCode").text(curCount + "s");
	InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次	
};
function checkPhone(phone) {//手机号的正则验证
	if((/^1(3|4|5|7|8|6|9)\d{9}$/.test(phone))) {
		return true;
	} else {
		return false;
	}
};
//点击验证码发送验证码结束**************************************************************************************/
$("#sign_num button").click(function(){//发送验证码短信的按钮
	var phone = $('#sign_phone_num').val();
	var pic_val = $("#pic_code").val();
	if(checkPhone(phone)){
		if(pic_val!=''){
			sendMessage(phone);
		}else{
			alert('请输入验证码!')
		}
	}else{
		alert('请输入正确的手机号!');
	}
})
$("#queren").click(function(){//点击确认绑定按钮
	var phone = $('#sign_phone_num').val();
	var code_num = $('#code_num').val();
	console.log('手机号:'+phone+' 验证码:'+code_num);
	$.ajax({
		type:"post",
		url:base_url+"ZITAOHUI/phoneVi",
		async:true,
		data:{
			"mobile":phone,
			"value":code_num
		},
		beforeSend:function(){
			$("#queren").attr("disabled", "true");//按钮不可用
		},
		success:function(data){
			if(data.code==1||data.code=='1'){
				bangding(phone);
			}else{
				alert("验证码错误,请重新发送或更改");	
				$("#queren").removeAttr("disabled"); //启用按钮
			}
		}
	});
})
function bangding(phone){
	var userMsg = JSON.parse(sessionStorage.userMsg);
	//console.log(userMsg);
	userMsg.user_phone = phone;
	console.log(userMsg);
	$.ajax({
		type:"post",
		url:base_url+"ZITAOHUI/user/saveUser",
		async:true,
		contentType: "application/json",
		data:JSON.stringify(userMsg),
		beforeSend:function(){
			$("#queren").attr("disabled", "true");//按钮不可用
		},
		success:function(data){
			console.log(data);
			sessionStorage.u_id = data.extend.user.user_Id;
			sessionStorage.uid = data.extend.user.user_Id;
			get_storeid();
			
			
		}
	});
}

function get_storeid() {//尝试获取storeid
	if(sessionStorage.u_id != undefined && sessionStorage.u_id != null) {
		$.ajax({
			url: base_url + 'ZITAOHUI/user',
			data: {
				userId: sessionStorage.getItem("uid"),
				value: 1
			},
			success: function(data) {
				var storeid = data.extend.user.storeId;
				if(storeid != null && storeid != '' && storeid != undefined) {
					sessionStorage.setItem("s_id", storeid);
					window.location.replace('../index.html');//跳转到首页
				}else{
					window.location.replace('../index.html');//跳转到首页
				}
			},
			error:function(){
				window.location.replace('../index.html');//跳转到首页
			}
		});
	}
}

function get_pic_num (){
	var userMsg = JSON.parse(sessionStorage.userMsg);
	var unionID = userMsg.unionID;
	console.log(unionID);
	$.ajax({
		type:"GET",
		url:base_url+"ZITAOHUI/verifyCode",
		async:true,
		data:{
			uid:unionID
		},
		success:function(data){
			console.log(data);
			var img_url ="data:image/png;base64," + data.extend.data;
			$('#pic_sign_url').attr("src",img_url);
		}
	});
}
get_pic_num();

$("#pic_sign_url").click(function(){
	get_pic_num();
})
